package com.lk.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.lk.springboot.service.LoginService;
import com.lk.springboot.service.SitesService;
import com.lk.springboot.vo.Result;
import com.lk.springboot.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lk
 * @date 2020/1/12 0012 15:13
 * @description: 站点兑换中心
 */
@RestController
public class SitesController {

    @Autowired
    SitesService sitesService;
    @Autowired
    LoginService loginService;

    /**
     * 站点兑换中心概述
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/sites/exchangecenter", method = RequestMethod.GET)
    public String siteExchangeCenterSumm(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = sitesService.getSummary();
            return result;
        }

    }

    /**
     * 站点兑换中心详情
     *
     * @param token
     * @param siru_id
     * @return
     */
    @RequestMapping(value = "/sites/exchangecenter/detail", method = RequestMethod.GET)
    public String siteExchangeCenter(@CookieValue("token") String token,
                                     @RequestParam(value = "siru_id") String siru_id) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = sitesService.getDetail(siru_id);
            return result;
        }

    }

    /**
     * 站点对换
     *
     * @param token
     * @param siru_id
     * @return
     */
    @RequestMapping(value = "/sites/exchange", method = RequestMethod.POST)
    public String siteExchange(@CookieValue("token") String token,
                               @RequestParam(value = "siru_id") String siru_id) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            String result = sitesService.sitesExchange(u_id, siru_id);
            return result;
        }

    }

    /**
     * 获取用户能用于兑换的站点数量
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getmysitesnumbest", method = RequestMethod.GET)
    public String getusersites(@CookieValue("token") String token) {
        String u_id = loginService.checkID(token);
        if (u_id == null) {
            Result ru = new Result(ResultCode.FAILD_LOGIN);
            ru.setData(" ");
            return JSON.toJSONString(ru);
        } else {
            Result result = new Result(ResultCode.SUCCESS);
            int sitesNum = sitesService.getBestSitesNum(u_id);
            result.setData(sitesNum);
            return JSON.toJSONString(result);
        }

    }
}
